package com.lz.sports.ui.group

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.lz.library.base.BaseFragment
import com.lz.library.extension.click
import com.lz.library.extension.resString
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lz.sports.BR
import com.lz.sports.R
import com.lz.sports.adapter.DeviceGroupAdapter
import com.lz.sports.bean.DeviceGroup
import com.lz.sports.bean.DeviceInfo
import com.lz.sports.databinding.FragmDeviceGroupListBinding
import com.lz.sports.dialog.EditPublicDialog
import com.lz.sports.dialog.WarningDialog
import com.lz.sports.vm.group.DeviceGroupListViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//设备分组列表
class DeviceGroupListFragment : BaseFragment<DeviceGroupListViewModel, FragmDeviceGroupListBinding>(),
    EditPublicDialog.OnEditConfirmListener, com.lz.library.adapter.BaseAdapter.OnItemClickListener,
    com.lz.library.adapter.BaseAdapter.OnItemLongClickListener, WarningDialog.OnWarningConfirmListener {

    private val adapter by lazy { DeviceGroupAdapter() }

    private var deletePosition = 0

    override fun getLayoutRes(): Int = R.layout.fragm_device_group_list

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        viewModel.deviceGroupList.observe(this) { adapter.setNewData(it) }
        viewModel.addDeviceGroup.observe(this) { adapter.addItem(item = it) }

        LiveEventBus.get("DeviceGroup_edit_name", DeviceGroup::class.java).observe(this) {
            adapter.updateGroupName(it)
        }
        LiveEventBus.get("DeviceGroup_bindDevice", DeviceInfo::class.java).observe(this) {
            adapter.bindDeviceInfo(it)
        }
        LiveEventBus.get("DeviceGroup_unbindAll", DeviceGroup::class.java).observe(this) {
            adapter.update(it.id)
        }
        LiveEventBus.get("DeviceGroup_unbind", DeviceInfo::class.java).observe(this) {
            adapter.unbindDeviceInfo(it)
        }
    }

    override fun initData(savedState: Bundle?) {
        binding.rvList.adapter = adapter
        adapter.setOnItemClickListener(this)
        adapter.setOnItemLongClickListener(this)
        lifecycleScope.launch {
            arrayOf(binding.tvAdd, binding.llAssist).click().collect {
                when (it.id) {
                    R.id.tv_add -> EditPublicDialog.show(
                        this@DeviceGroupListFragment,
                        R.string.group_name.resString(), R.string.group_new.resString()
                    )
                    R.id.ll_assist -> {
                        if (viewModel.assistDeviceInfoList.value != null && false) {
                            val bundle = Bundle().apply {
                                val list = viewModel.assistDeviceInfoList.value as ArrayList<DeviceInfo>
                                putParcelableArrayList("assistDeviceInfoList", list)
                            }
                            findNavController().navigate(R.id.action_list_to_edit, bundle)
                        }
                    }
                }
            }
        }
    }

    override fun onEditConfirm(dialog: EditPublicDialog, text: String, requestCode: Int) {
        dialog.dismiss()
        viewModel.addDeviceGroup(text)
    }

    override fun onResume() {
        super.onResume()
        adapter.data.forEach { it.resetBattery() }
    }

    override fun onItemClick(view: View, position: Int) {
        val bundle = Bundle().apply {
            putParcelable("deviceGroup", adapter.getItem(position))
        }
        findNavController().navigate(R.id.action_list_to_group_edit, bundle)
    }

    override fun onItemLongClick(view: View, position: Int): Boolean {
        deletePosition = position
        val deleteGroup = adapter.getItem(position)
        WarningDialog.show(this, resources.getString(R.string.group_delete, deleteGroup.name))
        return true
    }

    override fun onConfirm(dialog: WarningDialog, requestCode: Int) {
        dialog.dismiss()
        viewModel.delete(adapter.getItem(deletePosition).id)
        adapter.removeItem(deletePosition)
    }
}